﻿using System;
using System.Collections.Generic;
using GeoAPI.Geometries;

namespace YJKGridTopologyCal.Geometries.Utilities
{
	public class GeometryCollectionMapper
	{
		public static IGeometryCollection Map(IGeometryCollection gc, Func<IGeometry, IGeometry> op)
		{
			return new GeometryCollectionMapper(op).Map(gc);
		}

		public GeometryCollectionMapper(Func<IGeometry, IGeometry> mapOp)
		{
			this._mapOp = mapOp;
		}

		public IGeometryCollection Map(IGeometryCollection gc)
		{
			IList<IGeometry> list = new List<IGeometry>();
			for (int i = 0; i < gc.NumGeometries; i++)
			{
				IGeometry geometry = this._mapOp(gc.GetGeometryN(i));
				if (!geometry.IsEmpty)
				{
					list.Add(geometry);
				}
			}
			return gc.Factory.CreateGeometryCollection(GeometryFactory.ToGeometryArray(list));
		}

		private readonly Func<IGeometry, IGeometry> _mapOp;
	}
}
